import java.util.HashMap;
import java.util.Map;

class Solution {
    public int longestSubsequence(int[] arr, int difference) {
        int n = arr.length;
        Map<Integer, Integer> hash = new HashMap<>();
        int ret = 1;
        for (int i = 0; i < n; i++) {
            hash.put(arr[i], hash.getOrDefault(arr[i] - difference, 0) + 1);
            ret = Math.max(hash.get(arr[i]), ret);
        }
        return ret;
    }
}